package cn.iocoder.yudao.module.erp.service.contractpaymentplan;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.contractpaymentplandetail.ContractPaymentPlanDetailRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.contractpaymentplandetail.ContractPaymentPlanDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractpaymentplan.ContractPaymentPlanDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractpaymentplan.contractpaymentplandetail.ContractPaymentPlanDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.contractpaymentplan.ContractPaymentPlanMapper;
import cn.iocoder.yudao.module.erp.service.contractpaymentplan.contractpaymentplandetail.ContractPaymentPlanDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.CONTRACT_PAYMENT_PLAN_NOT_EXISTS;

/**
 * 合同付款计划 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ContractPaymentPlanServiceImpl implements ContractPaymentPlanService {

    @Resource
    private ContractPaymentPlanMapper contractPaymentPlanMapper;
    @Resource
    private ContractPaymentPlanDetailService contractPaymentPlanDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createContractPaymentPlan(ContractPaymentPlanSaveReqVO createReqVO) {
        //生成付款计划编号
        createReqVO.setPlanNo(IdUtil.simpleUUID());
        // 插入
        ContractPaymentPlanDO contractPaymentPlan = BeanUtils.toBean(createReqVO, ContractPaymentPlanDO.class);
        contractPaymentPlanMapper.insert(contractPaymentPlan);
        //插入计划内容
        List<ContractPaymentPlanDetailSaveReqVO> detailSaveReqVOS = createReqVO.getDetailSaveReqVOS();
        detailSaveReqVOS.forEach(x->{
            x.setPaymentPlanId(contractPaymentPlan.getId());
            x.setPlanNo(IdUtil.simpleUUID());
            contractPaymentPlanDetailService.createContractPaymentPlanDetail(x);
        });
        // 返回
        return contractPaymentPlan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractPaymentPlan(ContractPaymentPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateContractPaymentPlanExists(updateReqVO.getId());
        // 更新
        ContractPaymentPlanDO updateObj = BeanUtils.toBean(updateReqVO, ContractPaymentPlanDO.class);
        contractPaymentPlanMapper.updateById(updateObj);
        // 更新明细
        contractPaymentPlanDetailService.deleteByPaymentPlanId(updateReqVO.getId());
        List<ContractPaymentPlanDetailSaveReqVO> detailSaveReqVOS = updateReqVO.getDetailSaveReqVOS();
        detailSaveReqVOS.forEach(x->{
            x.setPaymentPlanId(updateReqVO.getId());
            x.setPlanNo(IdUtil.simpleUUID());
            contractPaymentPlanDetailService.createContractPaymentPlanDetail(x);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractPaymentPlan(Long id) {
        // 校验存在
        validateContractPaymentPlanExists(id);
        // 删除
        contractPaymentPlanMapper.deleteById(id);
        // 删除明细
        contractPaymentPlanDetailService.deleteByPaymentPlanId(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractPaymentPlanListByIds(List<Long> ids) {
        // 校验存在
        validateContractPaymentPlanExists(ids);
        // 删除
        contractPaymentPlanMapper.deleteByIds(ids);
        // 删除明细
        contractPaymentPlanDetailService.deleteByPaymentPlanIds(ids);
    }

    private void validateContractPaymentPlanExists(List<Long> ids) {
        List<ContractPaymentPlanDO> list = contractPaymentPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONTRACT_PAYMENT_PLAN_NOT_EXISTS);
        }
    }

    private void validateContractPaymentPlanExists(Long id) {
        if (contractPaymentPlanMapper.selectById(id) == null) {
            throw exception(CONTRACT_PAYMENT_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public ContractPaymentPlanRespVO getContractPaymentPlan(Long id) {
        ContractPaymentPlanRespVO respVO = BeanUtils.toBean(contractPaymentPlanMapper.selectById(id), ContractPaymentPlanRespVO.class);
        List <ContractPaymentPlanDetailDO> detailDOS = contractPaymentPlanDetailService.getByPaymentPlanId(id);
        List<ContractPaymentPlanDetailRespVO> detailRespVOS = BeanUtils.toBean(detailDOS, ContractPaymentPlanDetailRespVO.class);
        respVO.setDetailRespVOS(detailRespVOS);
        return respVO;
    }

    @Override
    public PageResult<ContractPaymentPlanDO> getContractPaymentPlanPage(ContractPaymentPlanPageReqVO pageReqVO) {
        return contractPaymentPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByContractId(Long contractId) {
        ContractPaymentPlanDO paymentPlanDO = contractPaymentPlanMapper.selectByContractId(contractId);
        if (paymentPlanDO == null){
            return;
        }
        //忽略  外部事务会生效 todo:测试一下
        this.deleteContractPaymentPlan(paymentPlanDO.getId());
    }

    @Override
    public void deleteByContractIds(List<Long> contractIds) {
        List<ContractPaymentPlanDO> paymentPlanDOS = contractPaymentPlanMapper.selectByContractIds(contractIds);
        if (CollUtil.isEmpty(paymentPlanDOS)){
            return;
        }
        List<Long> paymentPlanIds = paymentPlanDOS.stream()
                .map(ContractPaymentPlanDO::getContractId)
                .collect(Collectors.toList());
        //忽略  外部事务会生效 todo:测试一下
        this.deleteContractPaymentPlanListByIds(paymentPlanIds);
    }

    @Override
    public ContractPaymentPlanRespVO getByContractId(Long contractId) {
        ContractPaymentPlanDO paymentPlanDO = contractPaymentPlanMapper.selectByContractId(contractId);
        if (paymentPlanDO == null){
            return null;
        }
        return this.getContractPaymentPlan(paymentPlanDO.getId());
    }

}